/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
               
Revisions:

Author        Date    Description:

*/
using System;
using System.Threading;

namespace Cafechess.Threading
{
	/// <summary>
	/// Thread safe way to save and retrieve a boolean value.
	/// </summary>
  public class InterlockedBool
  {
    /// <summary>
    /// Default constructor.  Initializes the boolean value to false.
    /// </summary>
    public InterlockedBool() : this( false )
    {
    }

    /// <summary>
    /// Overloaded constructor.  Intializes the boolean value to a given value.
    /// </summary>
    /// 
    /// <param name="bInitialValue">
    /// The initial value of the boolean variable.
    /// </param>
    public InterlockedBool( bool bInitialValue )
    {
      Value = bInitialValue;
    }


    /// <summary>
    /// Provides synchronized access to the boolean value.
    /// </summary>
    public bool Value
    {
      set
      { 
        Interlocked.Exchange( ref cnValue, Convert.ToInt32( value ) );
      }
      get
      {
        int nValue = 0;
        Interlocked.Exchange( ref nValue, cnValue );
        return nValue == 1;
      }
    }


    private int cnValue;    // The value of the boolean variable.
  }
}
